#!/usr/bin/env python
"""
Axe Interpreter

An interpreter for programs written in the Axe Parser language for computers.

Author: Michael Lee (<michael.lee.0x2a@gmail.com>)
License: GNU Lesser GPL
"""

# System libraries
import sys
import cProfile
import pstats

# 3rd party libraries

# My libraries
import io
import axelex
import axeparse
import axeinterpreter
from meta import *

argparser = io.CommandIO()
options = argparser.parse()


class Axe():
    def __init__(self):
        self.lexer = axelex.build()
        self.parser = axeparse.build()
        self.interpreter = axeinterpreter.build(self.lexer, self.parser)
        return
    
    def run(self, text):
        assert(isinstance(text, basestring))
        if text[-1] != '\n' or text[-1] != ':':
            text = text + '\n'
        result = self.parser.parse(text, lexer=self.lexer)
        return self.interpreter.run(result)


text = """
.AA

0->B
For(A,0,2000):
    For(C,0,100):
        A->B
    End
End
Disp B
"""


text1 = """
.TEST

ClrDraw
20->X->Y

Repeat getKey(15)
    X - getKey(2) + getKey(3) -> X
    Y - getKey(4) + getKey(1) -> Y
    Rect(X,Y,5,5)
    DispGraph
    RectI(X,Y,5,5)
End
"""

axe = Axe()

def quick():
    global axe
    axe.run(text1)
    return

def test():
    cProfile.run('quick()', 'results')
    stats = pstats.Stats('results')
    stats.sort_stats('calls', 'nfl')
    stats.print_stats()

#main()
test()

sys.exit(0)









        

if options['interpret']:
    axeinterpreter.test_interpreter()
elif options['test']:
    choice = options['test']
    if options['input_path']:
        with open(options['input_path'], 'r') as file_name:
            text = file_name.read()
    else:
        text = ''
    if choice == 'io':
        io.test_emulator()
    elif choice == 'lex':
        axelex.test_lexer(text=text)
    elif choice == 'parse':
        axeparse.test_parser(text=text)
    elif choice == 'interpret':
        axeinterpreter.test_interpreter()
else:
    with open(options['input_path'], 'r') as file_name:
        text = file_name.read()
    axe = Axe()
    try:
        output = axe.run(text)
        print output
    except (ExitException, KeyboardInterrupt):
        print '\nEnding...\n'
    axeinterpreter.test_interpreter(interpreter=axe.interpreter)



